package ejb.utility;

import java.util.ArrayList;
import java.util.List;

import ejb.dto.EscursioneDTO;
import ejb.dto.GiftListDTO;
import ejb.dto.HotelDTO;
import ejb.dto.PacchettoDTO;
import ejb.dto.ProdottoBaseDTO;
import ejb.dto.UserDTO;
import ejb.dto.VoloDTO;
import ejb.entities.Basicproduct;
import ejb.entities.Escursion;
import ejb.entities.Flight;
import ejb.entities.Giftlist;
import ejb.entities.Hotel;
import ejb.entities.Pack;
import ejb.entities.User;

public class ConvertDTO {
	public static EscursioneDTO convertToEscursioneDTO(Escursion escursion) {
		EscursioneDTO escursioneDTO = new EscursioneDTO();
		escursioneDTO.setId(escursion.getBasicproduct().getBasicproductid());
		escursioneDTO.setDataInizio(escursion.getBasicproduct().getStartdate());
		escursioneDTO.setDataFine(escursion.getBasicproduct().getEnddate());
		escursioneDTO.setCitta(escursion.getCity());
		escursioneDTO.setOraInizio(escursion.getStarthour());
		escursioneDTO.setNposti(escursion.getBasicproduct().getPlaces());
		escursioneDTO.setPrezzo(escursion.getBasicproduct().getPrice());
		return escursioneDTO;
	}

	public static HotelDTO convertToHotelDTO(Hotel hotel) {
		HotelDTO hotelDTO = new HotelDTO();

		hotelDTO.setId(hotel.getBasicproduct().getBasicproductid());
		hotelDTO.setDataInizio(hotel.getBasicproduct().getStartdate());
		hotelDTO.setDataFine(hotel.getBasicproduct().getEnddate());
		hotelDTO.setNposti(hotel.getBasicproduct().getPlaces());
		hotelDTO.setPrezzo(hotel.getBasicproduct().getPrice());

		hotelDTO.setCitta(hotel.getCity());
		hotelDTO.setNome(hotel.getName());
		hotelDTO.setStelle(hotel.getStars());

		return hotelDTO;
	}

	public static VoloDTO convertToVoloDTO(Flight volo) {
		VoloDTO voloDTO = new VoloDTO();

		voloDTO.setId(volo.getBasicproduct().getBasicproductid());
		voloDTO.setDataInizio(volo.getBasicproduct().getStartdate());
		voloDTO.setDataFine(volo.getBasicproduct().getEnddate());
		voloDTO.setNposti(volo.getBasicproduct().getPlaces());
		voloDTO.setPrezzo(volo.getBasicproduct().getPrice());

		voloDTO.setCitta_partenza(volo.getDeparture());
		voloDTO.setCitta_arrivo(volo.getArrival());
		voloDTO.setCompagnia(volo.getCompany());
		voloDTO.setAereo(volo.getModel());

		return voloDTO;
	}

	public static GiftListDTO convertToDTOGiftList(Giftlist g) {
		GiftListDTO gDTO = new GiftListDTO();
		
		gDTO.setId(g.getIdgiftlist());

		// setta nome
		gDTO.setName(g.getName());

		// setta pacchetti
		for (Pack p : g.getPacks())
			gDTO.getPacchetti().add(convertToDTOPack(p));

		// setta pacchetti
		for (Basicproduct bp : g.getBasicproducts())
			gDTO.getProdottiBase().add(convertToDTOPb(bp));

		return gDTO;
	}

	public static PacchettoDTO convertToDTOPack(Pack p) {
		// pacchetto dto da riempire
		PacchettoDTO pDto = new PacchettoDTO();

		pDto.setDataFine(p.getEnddate());
		pDto.setDataInizio(p.getStartdate());
		pDto.setDescrizione(p.getDescription());
		pDto.setId(p.getPacksid());
		pDto.setNome(p.getName());
		pDto.setPrezzo(p.getPrice());
		pDto.setSconto(p.getDiscount());

		// prodotti base del pacchetto
		List<ProdottoBaseDTO> pbDtos = new ArrayList<ProdottoBaseDTO>();

		for (Basicproduct pbBasicproduct : p.getBasicproducts())
			System.out.println(pbBasicproduct.toString());

		for (Basicproduct pb : p.getBasicproducts())
			pbDtos.add(convertToDTOPb(pb));

		pDto.setProdottiBase(pbDtos);

		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();

		for (ProdottoBaseDTO pBaseDTO : pDto.getProdottiBase())
			System.out.println(pBaseDTO.toString());

		return pDto;
	}

	public static ProdottoBaseDTO convertToDTOPb(Basicproduct pb) {
		ProdottoBaseDTO pbDTO = new ProdottoBaseDTO();
		pbDTO.setDataInizio(pb.getStartdate());
		pbDTO.setDataFine(pb.getEnddate());
		pbDTO.setId(pb.getBasicproductid());
		return pbDTO;
	}

	public static UserDTO convertToDTOUser(User user) {
		UserDTO userDTO = new UserDTO();

		userDTO.setEmail(user.getUserid());
		userDTO.setCity(user.getCity());
		userDTO.setLastName(user.getSurname());
		userDTO.setBirthDate(user.getBirth());
		userDTO.setAddress(user.getAddress());
		userDTO.setCountry(user.getCountry());
		userDTO.setPassword(user.getPassword());
		return userDTO;
	}
}
